/**
 * Created by liuqingxia on 2016/6/7.
 */

var url = getRoot() + "/function/find";
var urlWithUrlParam = getUrlBySuffix("/function/findByUrl");
var currentId;
var funcTable;
var funcTableWithUrl;
$(document).ready(function () {

    LTS.colFormatter.optFormat = function (v, row) {
    	return '<a href="javascript:;" class="update-btn"><span class="label label-success" style="font-size: 14px"><i class="fa fa-edit"></i>编辑</span><span class="hidden lts-data">' + JSON.stringify(row) + '</span></a>&nbsp;' +
        '<a href="javascript:;" class="remove-btn"><span class="label label-primary" style="background-color: #DD6B55;font-size: 14px"><i class="fa fa-trash-o"></i>删除</span></a>&nbsp;';
    }

    LTS.colFormatter.methodTypeFormat = function (v, row) {
        return transIntMethodType(row);
    }
    LTS.colFormatter.moduleTypeFormat = function(v,row){
    	
    }
    funcTable = $("#function-table-container").ltsTable({
        url: url,
        templateId: 'function-table'
    });

    funcTableWithUrl = $("#function-table-container").ltsTable({
        url: urlWithUrlParam,
        templateId: 'function-table'
    });

    funcTable.post({}, 1);

    clickSearchAllBtn();
    clickSearchBtn();
    clickRemoveBtn();
    clickConfirmRemoveBtn();

    updateClick();
    confirmUpdateClick();
    
    batchClick();
    
});

function batchClick(){
	$(document).on("click","#batchUpdate-btn",function(){
		var tableName = $("#tableName").val();
		var fieldName = $("#fieldName").val();
		var url = getRoot() + "/function/batchUpdateData";
		$.post(url,{"tableName":tableName,"fieldName":fieldName},function(data){
			if(data.success){
				swal("成功",data.root,"success");
			}
			if(data.failure){
				swal("失败",data.root,"error");
			}
		});
	});
}

/**
 * 编辑功能按钮
 * */


/**
 * 点击搜索全部按钮
 */
function clickSearchAllBtn() {
    $(document).on("click", "#searchAll", function () {
        funcTable.post({}, 1);
    });
}

/**
 * 点击确认删除按钮
 */
function clickConfirmRemoveBtn() {
    $(document).on("click", "#confirm-remove-btn", function () {

        $("#cancel-remove-btn").trigger("click");

        var params = {};
        params['id'] = parseInt($.trim(currentId));
        var url = getUrlBySuffix("/function/remove");

        $.postHttp(params, url, function (json) {
            if (json && json.success) {
            	
            	swal({
    				title : "操作成功",
    				text : json['message'],
    				type : "success"
    			}, function() {
                    $("#remove-tip-modal").modal("hide");
                    funcTable.post({}, pageNum);
    			});
            	
            } else if (json) {
                swal("失败", json['message'], 'error');
            } else {
                swal("失败", "未能从服务器获取正确信息", 'error');
            }
        });
        currentId = "";
    });
}

/**
 * 点击删除按钮
 */
function clickRemoveBtn() {
    $(document).on("click", ".remove-btn", function () {
        currentId = getCurrentId($(this));
        $("#remove-tip-modal").modal("show");
    });
}


/**
 * methodType翻译
 * @param row
 * @returns {string}
 */
function transIntMethodType(row) {
    var typeInt = row['methodType'];
    if (typeInt == 0) {
        return "get";
    }

    if (typeInt == 1) {
        return "post";
    }

    return "未知";
}


/**
 * 点击搜索按钮
 */
function clickSearchBtn() {
    $(document).on("click", "#searchBtn", function () {
        var params = {};
        var url = $.trim($("#url").val());
        params['url'] = url;
        funcTableWithUrl.post(params, 1);
    });
}

/**
 * 编辑按钮
 * */
function updateClick(){
	$(document).on("click",".update-btn",function(){
        var funcJson = $(this).find("span.lts-data").html();
        funcJson = $.trim(funcJson);
        var func = JSON.parse(funcJson);
        $.each($('#editForm').parent().find(".form-control"), function () {
            var name = $(this).attr("name");
            if(isNull(name)){
                return true;
            }
            name = $.trim(name);
            var value = func[name];
            $(this).val(value);
        });
        $("#function-edit-modal").modal("show");
    });
}

/**
 * 编辑确认按钮
 * */
function confirmUpdateClick(){
	$(document).on("click","#edit-btn",function(){
		var params = {};
        $.each($('#editForm').parent().find(".form-control"), function () {
            var name = $(this).attr("name");
            if(isNull(name)){
                return true;
            }
            var value = $(this).val();
            if(!isNull(value)){
                value = $.trim(value);
                params[name] = value;
            }
        });
        params['moduleName'] = $("#moduleType option:selected").attr("alt");
        var url = getRoot() + "/function/update";
        $.postHttp(params,url,function(json){
            if (json && json.success) {
                swal({
    				title : "操作成功",
    				text : json['message'],
    				type : "success",
    				confirmButtonText : "确定"
    			}, function() {
    				var param = {};
                    var url = $.trim($("#url").val());
                    param['url'] = url;
                    $("#function-edit-modal").modal("hide");
                    if(param['url']==""){
                    	funcTable.post({}, pageNum);
                    }else{
                    	funcTableWithUrl.post(param, pageNum);
                    }
    			});
            } else {
                if (json) {
                    swal('失败',json['message'], 'error');
                    $("#function-edit-modal").modal("hide");
                }
            }
        });
	});
}
